package com.kobeliu.service.impl;

import com.kobeliu.controller.request.LinkGroupAddRequest;
import com.kobeliu.controller.request.LinkGroupUpdateRequest;
import com.kobeliu.enums.BizCodeEnum;
import com.kobeliu.interceptor.LoginInterceptor;
import com.kobeliu.manager.LinkGroupManager;
import com.kobeliu.model.LinkGroupDO;
import com.kobeliu.mapper.LinkGroupMapper;
import com.kobeliu.model.LoginUser;
import com.kobeliu.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kobeliu.util.JsonData;
import com.kobeliu.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kobeliu
 * @since 2024-06-14
 */
@Service
public class LinkGroupServiceImpl implements LinkGroupService {


    @Autowired
    private LinkGroupManager linkGroupManager;


    @Override
    public int add(LinkGroupAddRequest addRequest) {

        //拿取身份信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        //组装对象
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(addRequest.getTitle());

        return linkGroupManager.add(linkGroupDO);

    }

    @Override
    public int del(Long groupId) {

        //防止水平越权，不能直接删除
        Long accountNo= LoginInterceptor.threadLocal.get().getAccountNo();

        int del = linkGroupManager.del(groupId, accountNo);

        return del;
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        //防止水平越权，不能直接返回详情
        Long accountNo= LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId);
        LinkGroupVO linkGroupVO = new LinkGroupVO();

        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> findAll() {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOS = linkGroupManager.findAll(accountNo);

        List<LinkGroupVO> linkGroupVOS = linkGroupDOS.stream().map(obj -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj,linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());

        return linkGroupVOS;
    }

    @Override
    public int update(LinkGroupUpdateRequest request) {
        //校验身份信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setId(request.getId());

        return linkGroupManager.update(linkGroupDO);

    }
}
